import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'  // 引入path模块处理路径

export default defineConfig({
  plugins: [vue()],
  // 新增：配置路径别名
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')  // 将@指向src目录
    }
  },
  // 配置需要被视为静态资源的文件类型
  assetsInclude: ['**/*.woff', '**/*.woff2', '**/*.eot', '**/*.ttf', '**/*.otf'],

  // 开发服务器配置
  server: {
    port: 8081,
    // 配置 CORS，允许跨域访问外部 API
    cors: true,
    proxy: {
      // 追踪服务读接口代理
      '/api/traceapiread': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
        secure: false
      },
      // 代理费用管理API读接口 - 使用setapiread
      '/setapiread': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        rewrite: (path) => path,
        configure: (proxy, options) => {
          console.log('代理 /setapiread 配置:', options)
        }
      },
      // 代理费用管理API写接口 - 使用setapiwrite
      '/setapiwrite': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        rewrite: (path) => path,
        configure: (proxy, options) => {
          console.log('代理 /setapiwrite 配置:', options)
        }
      },
      // 代理追踪管理API读接口
      '/api/traceapiread': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        rewrite: (path) => {
          console.log('代理前:', path)
          const newPath = path.replace(/^\/api/, '')
          console.log('代理后:', newPath)
          return newPath
        },
        secure: false
      },
      '/traceapiread': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        rewrite: (path) => path,
        secure: false
      },
      // 追踪服务写接口代理
      '/api/traceapiwrite': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        rewrite: (path) => {
          console.log('写代理前:', path)
          const newPath = path.replace(/^\/api/, '')
          console.log('写代理后:', newPath)
          return newPath
        },
        secure: false,
        configure: (proxy, options) => {
          proxy.on('error', (err, req, res) => {
            console.log('写代理错误:', err)
          })
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('写代理请求:', req.method, req.url, '-> ', options.target + proxyReq.path)
          })
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('写代理响应:', proxyRes.statusCode)
          })
        }
      },
      // 代理物资管理API读接口
      '/api/setapiread': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
        secure: false
      },
      // 代理物资管理API写接口
      '/api/setapiwrite': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
        secure: false
      },
      // 代理物料管理API读接口
      '/api/mateapiread': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
        secure: false
      },
      // 代理物料管理API写接口
      '/api/mateapiwrite': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
        secure: false
      },
      '/WeatherForecast': {
        target: 'http://localhost:5049',
        changeOrigin: true,
      },
      // 代理费用管理API读接口
      '/feeread': {
        target: 'http://localhost:62805',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/feeread/, '')
      },
      // 代理费用管理API写接口
      '/feewrite': {
        target: 'http://localhost:5863',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/feewrite/, '')
      },
      // 车辆服务读接口代理
      '/carservread': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        rewrite: (path) => path
      },
      // AI 服务代理
      '/fr': {
        target: 'http://10.223.7.18:8075',
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/fr/, '')
      },
      // 天气服务代理
      '/seniverse': {
        target: 'https://api.seniverse.com',
        changeOrigin: true,
        secure: true,
        rewrite: (path) => path.replace(/^\/seniverse/, '')
      },
      // 系统管理API代理（登录、角色、用户等）
      '/systemmanagereadapi': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        secure: false
      },
      // 系统管理写API代理（菜单添加、删除等）
      '/systemmanagewriteapi': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        secure: false
      },
      // 审批引擎读服务代理
      '/ExamineEngieRead': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/ExamineEngieRead/, '/ExamineEngieRead/api'),
        secure: false
      },
      // 审批引擎写服务代理
      '/ExamineEngieWrite': {
        target: 'http://115.190.179.32:8000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/ExamineEngieWrite/, '/ExamineEngieWrite/api'),
        secure: false
      }
    }
  },

  build: {
    rollupOptions: {
      output: {
        // 配置字体文件的输出路径和命名规则
        assetFileNames: (assetInfo) => {
          if (assetInfo.name?.endsWith('.woff') || assetInfo.name?.endsWith('.woff2') ||
            assetInfo.name?.endsWith('.eot') || assetInfo.name?.endsWith('.ttf') ||
            assetInfo.name?.endsWith('.otf')) {
            return `fonts/[name].[ext]`
          }
          return `assets/[name].[ext]`
        }
        
      }
    }
  }
})
